/**
 * Created by PanJiaChen on 16/11/18.
 */
import { getLabel, getLabelCategory,getDictData,getAllUser,getUserList } from 'api/index'
import {store} from 'redux/store'

export function transListToTreeData(list, value) {
  // console.log(list, value)
  const arr = [] // 搜集找到的结果
  list.forEach(item => {
    if (item.pid === value) {
      const children = transListToTreeData(list, item.id)
      if (children.length > 0) {
        item.children = children
      }
      arr.push(item)
    }
  })
  return arr
}

// 标签分类和标签合并
export function label(label, labelCategory) {
  const data = labelCategory.result.records
  data.forEach(item => {
    label.result.forEach(i => {
      if (i.labelCategoryId === item.id) {
        if (!item.children) {
          item.children = []
          item.children.push(i)
        } else {
          item.children.push(i)
        }
      }
    })
  })

  return data.filter(i => !i.labelCategoryId)
}

// // 标签分类和标签合并
// export function labelAll(label, labelCategory) {
//   const data = labelCategory.result.records
//   data.forEach(item => {
//     label.result.forEach(i => {
//       if (i.labelCategoryId === item.id) {
//         if (!item.children) {
//           item.children = []
//           item.children.push(i)
//         } else {
//           item.children.push(i)
//         }
//       }
//     })
//   })
//   return data.filter(i => !i.labelCategoryId)
// }

// 获取字典数据
export function getDictionaries(setDictionaries) {
  const fn = async() => {
    const res1 = await getLabel()
    const res2 = await getAllUser() // 获取用户种类
    const res3 = await getUserList({
      userType: 2,
      roleId: 6,
      pageSize: 200
    })
    // 词性
    var p1 = new Promise((resolve, reject) => {
      getDictData('PartOfSpeech').then((res) => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
    var p2 = new Promise((resolve, reject) => {
      getDictData('Difficulty').then((res) => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
    var p3 = new Promise((resolve, reject) => {
      getDictData('PublishStatus').then((res) => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
    var p4 = new Promise((resolve, reject) => {
      getDictData('LexileValue').then((res) => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
    var p5 = new Promise((resolve, reject) => {
      getDictData('Publisher').then((res) => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
    var p6 = new Promise((resolve, reject) => {
      getDictData('BookType').then((res) => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
    var p7 = new Promise((resolve, reject) => {
      getDictData('ReadMode').then((res) => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
    var p8 = new Promise((resolve, reject) => {
      getDictData('AgeRange').then((res) => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
    var p9 = new Promise((resolve, reject) => {
      getDictData('SeriesName').then((res) => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
    var p10 = new Promise((resolve, reject) => {
      getDictData('Theme').then((res) => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
    var p11 = new Promise((resolve, reject) => {
      getDictData('CaptionPosition').then((res) => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
    var p12 = new Promise((resolve, reject) => {
      getDictData('PageType').then((res) => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
    var p13 = new Promise((resolve, reject) => {
      getDictData('question_category').then((res) => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
    var p14 = new Promise((resolve, reject) => {
      getDictData('question_type').then((res) => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
    var p15 = new Promise((resolve, reject) => {
      getDictData('paper_type').then((res) => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
    var p16 = new Promise((resolve, reject) => {
      getDictData('AVType').then((res) => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
    var p17 = new Promise((resolve, reject) => {
      getDictData('AVTime').then((res) => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
    var p18 = new Promise((resolve, reject) => {
      getDictData('AVSerial').then((res) => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
    var p19 = new Promise((resolve, reject) => {
      getDictData('GameType').then((res) => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
    var p20 = new Promise((resolve, reject) => {
      getLabelCategory().then((res) => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
    var p21 = new Promise((resolve, reject) => {
      getDictData('question_channel').then((res) => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
    var p22 = new Promise((resolve, reject) => {
      getDictData('bookAuthor').then((res) => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
    var p23 = new Promise((resolve, reject) => {
      getDictData('OldAgeRange').then((res) => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
    var p24 = new Promise((resolve, reject) => {
      getDictData('ExploreGrade').then((res) => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
    var p25 = new Promise((resolve, reject) => {
      getDictData('AppAcountType').then((res) => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
    var p26 = new Promise((resolve, reject) => {
      getDictData('includeSubject').then((res) => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
    var p27 = new Promise((resolve, reject) => {
      getDictData('LevelApp').then((res) => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
    var p28 = new Promise((resolve, reject) => {
      getDictData('RankType').then((res) => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
    var p29 = new Promise((resolve, reject) => {
      getDictData('LinkTypeOfLearn').then((res) => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
    var p30 = new Promise((resolve, reject) => {
      getDictData('LinkTypeOfLesson').then((res) => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
    var p31 = new Promise((resolve, reject) => {
      getDictData('LinkTypeOfSubtask').then((res) => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
    var p32 = new Promise((resolve, reject) => {
      getDictData('ExaminationType').then((res) => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })

    var p33 = new Promise((resolve, reject) => {
      getDictData('Grammar').then((res) => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })

    Promise.all([p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16, p17, p18, p19, p20, p21, p22, p23, p24, p25, p26, p27, p28, p29, p30, p31, p32, p33]).then((res) => {
      const data = {
        PartOfSpeech: res[0].result,
        Label: label(res1, res[19]),
        QandPQ:res3.result.records.map((item => {
        return {
          id: item.id,
          userName: item.userName
        }})),
        Level: res[1].result,
        wordStatus: res[2].result,
        Lexile: res[3].result,
        Publisher: res[4].result,
        BookType: res[5].result,
        ReadMode: res[6].result,
        AgeRange: res[7].result,
        SeriesName: res[8].result,
        Theme: res[9].result,
        CaptionPosition: res[10].result,
        PageType: res[11].result,
        GameType: res[18].result,
        paperType: res[14].result,
        AvSerial: res[17].result,
        AVType: res[15].result,
        Duration: res[16].result,
        UserSpecies: res2.result,
        CoreWord: res[20].result,
        bookAuthor: res[21].result,
        originalGradeId: res[22].result,
        LevelExplore: res[23].result,
        AppAcountType: res[24].result,
        isSubject: res[25].result,
        LevelApp: res[26].result,
        RankType: res[27].result,
        LinkTypeOfLearn: res[28].result,
        LinkTypeOfLesson: res[29].result,
        LinkTypeOfSubtask: res[30].result,
        SubjectType: res[31].result.map(i => {
          if (i.dictValue === '语法') {
            i.children = res[32].result
          }
          return i
        }),
        Publish: [
          { id: 1, dictValue: '已上架' },
          { id: 2, dictValue: '已下架' }
        ],
        CaptionIn: [
          { id: 0, dictValue: '无' },
          { id: 1, dictValue: '有' }
        ],
        readTypeList: [
          {
            id: 1,
            dictValue: '精读'
          },
          {
            id: 2,
            dictValue: '泛读'
          }
        ],
        planType: [
          { id: 1, dictValue: '单词' },
          { id: 2, dictValue: '主题课' },
          { id: 3, dictValue: '课后练习' }
        ],
        QuestionType: [
          { id: 1, dictValue: '单选题' },
          { id: 2, dictValue: '判断题' },
          { id: 4, dictValue: '排序题' },
          { id: 5, dictValue: '连线题' },
          { id: 6, dictValue: '识读题' }
        ],
        QuestionCategory: [
          { id: 3, dictValue: '看文选文' },
          { id: 4, dictValue: '看文选图' },
          { id: 9, dictValue: '看文选音' },
          { id: 1, dictValue: '听音选文' },
          { id: 2, dictValue: '听音选图' },
          { id: 10, dictValue: '听音选音' },
          { id: 8, dictValue: '听音跟读' },
          { id: 5, dictValue: '看图选文' },
          { id: 6, dictValue: '看图跟读' },
          { id: 7, dictValue: '看文跟读' }
        ],
        Recycling: [
          { id: 1, dictValue: '未回收' },
          { id: 2, dictValue: '已回收' }
        ],
        shareOption: [
          { id: false, dictValue: '不可分享' },
          { id: true, dictValue: '可分享' }
        ],
        CountNumber: [
          { id: 0, dictValue: '0-5' },
          { id: 5, dictValue: '5-10' },
          { id: 10, dictValue: '10-15' },
          { id: 15, dictValue: '15-20' },
          { id: 20, dictValue: '20-25' },
          { id: 25, dictValue: '25-30' },
          { id: 30, dictValue: '30-35' },
          { id: 35, dictValue: '35-40' },
          { id: 40, dictValue: '40-45' },
          { id: 45, dictValue: '45-50' },
          { id: 50, dictValue: '50以上' }
        ],

        WordInfoIsNull: [
          { id: 1, dictValue: '级别为空' },
          { id: 2, dictValue: '词性为空' },
          { id: 3, dictValue: '中文为空' }
        ],
        AvInfoIsNull: [
          { id: 1, dictValue: '类型为空' }
        ],
        isDub: [
          { id: 0, dictValue: '否' },
          { id: 1, dictValue: '是' }
        ],
        SourceOption: [
          { id: 1, dictValue: '绘本' },
          { id: 5, dictValue: '体系课' },
          { id: 3, dictValue: '试卷' }
        ],
        linkType: [
          { id: 2001, dictValue: '互动课堂' },
          { id: 2002, dictValue: '绘本精读' },
          { id: 2003, dictValue: '视频学习' },
          { id: 2004, dictValue: '拓展视频' },
          { id: 2005, dictValue: '拓展绘本' },
          { id: 2006, dictValue: '绘本配音' },
          { id: 2007, dictValue: '动画配音' },
          { id: 2008, dictValue: '听力练习' }
        ],
        BookInfoIsNull: [
          { id: 1, dictValue: '核心单词' },
          { id: 2, dictValue: '主题' },
          { id: 3, dictValue: '蓝思值' },
          { id: 4, dictValue: '级别' },
          { id: 5, dictValue: '阅读模式' },
          { id: 6, dictValue: '字幕类型' },
          { id: 7, dictValue: '开页方式' },
          { id: 8, dictValue: '分享' }
        ]
      }
      setDictionaries(data)
    })
  }
  
  fn()
}

// 获取标签数据
export function getLabelOption() {
  const data = store.getState().dictionaries.Label
  return data
}
